package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.BaseTimeDialogFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.base.BaseYMDTimeDialogFragment;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.Date;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2016/12/21 19:02
 */

public class QueryDateFragment extends BaseFragment {
  /**
   * 接收的
   */
  public static final String EXTRA_START_DATE =
      "com.isunland.intelligentFarmbyWJ.ui.QueryDateFragment.EXTRA_START_DATE";
  public static final String EXTRA_END_DATE =
      "com.isunland.intelligentFarmbyWJ.ui.QueryDateFragment.EXTRA_END_DATE";

  private static final int REQUEST_START_DATE = 1;//开始日期
  private static final int REQUEST_END_DATE = 2;//结束日期

  @BindView(R.id.tv_startTime_workConvert) TextView mStartDateTv;
  @BindView(R.id.tv_endTime_workConvert) TextView mEndDateTv;
  private Date mStartDate;
  private Date mEndDate;
  private FragmentManager mFm;

  public static Fragment newInstance(Date mStartDate, Date mEndDate) {

    QueryDateFragment fragment = new QueryDateFragment();
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_START_DATE, mStartDate);
    args.putSerializable(EXTRA_END_DATE, mEndDate);
    fragment.setArguments(args);
    return fragment;
  }

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom(R.string.query);
    mStartDate = (Date) getArguments().getSerializable(EXTRA_START_DATE);
    mEndDate = (Date) getArguments().getSerializable(EXTRA_END_DATE);
    mActivity = (BaseVolleyActivity) getActivity();
    mFm = getActivity().getSupportFragmentManager();
  }

  //初始化界面
  @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_query_date, container, false);
    ButterKnife.bind(this, v);
    mStartDateTv.setText(MyDateUtil.parseDateToString(mStartDate));
    mEndDateTv.setText(MyDateUtil.parseDateToString(mEndDate));
    return v;
  }

  @OnClick({ R.id.tv_startTime_workConvert, R.id.tv_endTime_workConvert })
  public void click(View view) {
    if (view == null) return;
    DialogFragment dialog;
    switch (view.getId()) {
      case R.id.tv_startTime_workConvert:
        dialog = BaseYMDTimeDialogFragment.newInstance(mStartDate);
        dialog.setTargetFragment(this, REQUEST_START_DATE);
        break;
      case R.id.tv_endTime_workConvert:
        dialog = BaseYMDTimeDialogFragment.newInstance(mEndDate);
        dialog.setTargetFragment(this, REQUEST_END_DATE);
        break;
      default:
        return;
    }

    dialog.show(mFm, "");
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) return;
    if (requestCode == REQUEST_START_DATE && data != null) {
      mStartDate = (Date) data.getSerializableExtra(BaseTimeDialogFragment.EXTRA_DATE);
      mStartDateTv.setText(MyDateUtil.parseDateToString(mStartDate));

      return;
    }
    if (requestCode == REQUEST_END_DATE && data != null) {
      mEndDate = (Date) data.getSerializableExtra(BaseTimeDialogFragment.EXTRA_DATE);
      mEndDateTv.setText(MyDateUtil.parseDateToString(mEndDate));

      return;
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // 响应返回按钮
      case android.R.id.home:
        if (!getActivity().isFinishing()) getActivity().finish();

        break;
      case R.id.menu_item_confirm:
        if (mStartDate == null || mEndDate == null) {
          ToastUtil.show("请选择日期");
          return false;
        }
        if (mStartDate.after(mEndDate)) {
          ToastUtil.show("请选择正确的时间范围");
          return false;
        }

        Intent intent = new Intent();
        intent.putExtra(EXTRA_START_DATE, mStartDate);
        intent.putExtra(EXTRA_END_DATE, mEndDate);

        getActivity().setResult(Activity.RESULT_OK, intent);
        if (!getActivity().isFinishing()) getActivity().finish();
        return true;

      default:
        break;
    }

    return super.onOptionsItemSelected(item);
  }
}